package com.ling.core.view.builder;

import com.bstek.dorado.view.widget.base.Button;
import com.ling.core.view.ViewComponent;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author bo.wang
 * @since 2013-2-18
 */
@Component
public class ButtonBuilder extends AbstractControlBuilder {
    public void build(Object control, ViewComponent parentViewComponent) {
        Button button = (Button) control;
        String id = button.getId();
        ViewComponent component = null;
        if (StringUtils.isNotEmpty(id)) {
            component = generateViewComponent(id, Button.class);
            if (StringUtils.isNotEmpty(button.getCaption())) {
                component.setDesc(button.getCaption());
            }
        } else if (StringUtils.isNotEmpty(button.getCaption())) {
            component = generateViewComponent(button.getCaption(), Button.class);
        }
        if (component != null && StringUtils.isEmpty(component.getId())) {
            component.setEnabled(false);
        }
        if (component != null) {
            parentViewComponent.addChildren(component);
        }
    }

    public boolean support(Object control) {
        return control instanceof Button;
    }
}
